/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
 //Xiong@2011.11.19:  merge all apps sliding view

package com.android.launcherplus;

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.graphics.drawable.TransitionDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ImageView;

public class PreviewPager extends ViewGroup {
	private int mTotalItems;
	private int mCurrentItem;
	private int mDotDrawableId;
	private int[] mDotDrawableIds = new int[9];

	private static final int ORIENTATION_HORIZONTAL = 1;
	private int mOrientation = ORIENTATION_HORIZONTAL;
	
	public PreviewPager(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		initPager();
	}

	public PreviewPager(Context context) {
		super(context);
		initPager();
		// TODO Auto-generated constructor stub
	}
	private void initPager(){
		setFocusable(false);
		setWillNotDraw(false);
		mDotDrawableId=R.drawable.pager_dots;

		mDotDrawableIds[0] = R.drawable.pager_dots_1;
		mDotDrawableIds[1] = R.drawable.pager_dots_2;
		mDotDrawableIds[2] = R.drawable.pager_dots_3;
		mDotDrawableIds[3] = R.drawable.pager_dots_4;
		mDotDrawableIds[4] = R.drawable.pager_dots_5;
		mDotDrawableIds[5] = R.drawable.pager_dots_6;
		mDotDrawableIds[6] = R.drawable.pager_dots_7;
		mDotDrawableIds[7] = R.drawable.pager_dots_8;
		mDotDrawableIds[8] = R.drawable.pager_dots_9;

	}
	
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		if(mTotalItems<=0) return;
		if (mOrientation != ORIENTATION_HORIZONTAL) {
			createVerticalLayout();
		} else {
		    createHorizontalLayout();
		}
		//createLayout();
	}
	private void updateLayout(){
		for(int i=0;i<getChildCount();i++){
			final ImageView img=(ImageView) getChildAt(i);
			TransitionDrawable tmp=(TransitionDrawable)img.getDrawable();
			if(i==mCurrentItem){
				tmp.startTransition(50);
			}else{
				tmp.resetTransition();
			}
		}
	}

	private void createVerticalLayout(){
		detachAllViewsFromParent();
		
		int dotWidth=getResources().getDrawable(mDotDrawableId).getIntrinsicWidth();
		int separation=0; 
		int marginTop=((getHeight())/2)-(((mTotalItems*dotWidth)/2)+(((mTotalItems-1)*separation)/2));
		int marginLeft=((getWidth())/2)-(dotWidth/2);
		for(int i=0;i<mTotalItems;i++){
			ImageView dot=new ImageView(getContext());
			TransitionDrawable td;
			td=(TransitionDrawable)getResources().getDrawable(mDotDrawableId);
			td.setCrossFadeEnabled(true);
			dot.setImageDrawable(td);
	        ViewGroup.LayoutParams p;
	        p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
	        		ViewGroup.LayoutParams.FILL_PARENT);
            dot.setLayoutParams(p);
            int childHeightSpec = getChildMeasureSpec(
                    MeasureSpec.makeMeasureSpec(dotWidth, MeasureSpec.UNSPECIFIED), 0, p.height);
            int childWidthSpec = getChildMeasureSpec(
                    MeasureSpec.makeMeasureSpec(dotWidth, MeasureSpec.EXACTLY), 0, p.width);
            dot.measure(childWidthSpec, childHeightSpec);
			
            int top=marginTop+(i*(dotWidth+separation));
            
            
			dot.layout(marginLeft, top, marginLeft+dotWidth,top+dotWidth );
            addViewInLayout(dot, getChildCount(), p, true);
            if(i==mCurrentItem){
            	TransitionDrawable tmp=(TransitionDrawable)dot.getDrawable();
            	tmp.startTransition(200);
            }
		}
		postInvalidate();
	}

	
	private void createHorizontalLayout(){
		detachAllViewsFromParent();
		
		int dotWidth=getResources().getDrawable(mDotDrawableId).getIntrinsicWidth();
		int separation=20;  
		int marginLeft=((getWidth())/2)-(((mTotalItems*dotWidth)/2)+(((mTotalItems-1)*separation)/2));
		int marginTop=((getHeight())/2)-(dotWidth/2);
		for(int i=0;i<mTotalItems;i++){
			ImageView dot=new ImageView(getContext());
			TransitionDrawable td;
			//td=(TransitionDrawable)getResources().getDrawable(mDotDrawableId);
			td=(TransitionDrawable)getResources().getDrawable(mDotDrawableIds[i]);
			td.setCrossFadeEnabled(true);
			dot.setImageDrawable(td);
	        ViewGroup.LayoutParams p;
	        p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
	        		ViewGroup.LayoutParams.FILL_PARENT);
            dot.setLayoutParams(p);
            int childHeightSpec = getChildMeasureSpec(
                    MeasureSpec.makeMeasureSpec(dotWidth, MeasureSpec.UNSPECIFIED), 0, p.height);
            int childWidthSpec = getChildMeasureSpec(
                    MeasureSpec.makeMeasureSpec(dotWidth, MeasureSpec.EXACTLY), 0, p.width);
            dot.measure(childWidthSpec, childHeightSpec);
			
            int left=marginLeft+(i*(dotWidth+separation));
            
            
			dot.layout(left, marginTop, left+dotWidth,marginTop+dotWidth );
            addViewInLayout(dot, getChildCount(), p, true);
            if(i==mCurrentItem){
            	TransitionDrawable tmp=(TransitionDrawable)dot.getDrawable();
            	tmp.startTransition(200);
            }
		}
		postInvalidate();
	}
	protected int getTotalItems() {
		return mTotalItems;
	}

	protected void setTotalItems(int totalItems) {
		if(totalItems!=mTotalItems){
			this.mTotalItems = totalItems;
			if (mOrientation != ORIENTATION_HORIZONTAL) {
				createVerticalLayout();
			} else {
			    createHorizontalLayout();
			}
			//createLayout();
		}
	}

	protected int getCurrentItem() {
		return mCurrentItem;
	}

	protected void setCurrentItem(int currentItem) {
		if(currentItem!=mCurrentItem){
			this.mCurrentItem = currentItem;
			updateLayout();
		}
	}
	protected void setLeft(int value){
		int width=this.mRight-this.mLeft;
		this.mLeft=value;
		this.mRight=this.mLeft+width;
	}
	protected void setDirection(int direction){
		mOrientation = direction;
	}
}
